/**
 * Created by shidaiapp on 2017/9/29.
 */
import md5 from '../../../../assets/js/md5.js'

function resetForm(self,tiyData) {
  self.$refs['paramUser'].resetFields();
  self.$refs["roleSelect"].clearSingleSelect();
}


function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalModify.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalModify.isLoading = true;
  });

  tiyData.tStatus.modalDelete.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDelete.isLoading = true;
  });

}

/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;

};

/**
 * 添加新员工
 * @param self
 * @param tiyData
 */
export const add = (self, tiyData) => {
  resetForm(self, tiyData);
  tiyData.tParam.user.id = "";
  tiyData.tParam.user.userName = "";
  tiyData.tParam.user.roleId = "";
  tiyData.tParam.user.roleName = "";
  tiyData.tParam.user.access = "";
  tiyData.tParam.user.password = "";
  tiyData.tParam.user.passwordCheck = "";
  tiyData.tParam.user.roleList = [];
  self.paramUser.roles = [];
  tiyData.tStatus.modalModify.isShow = true;
  tiyData.tStatus.modalModify.isModify = false;
  tiyData.tStatus.modalModify.title =   "新建员工";
  self.tempRole = {};

};
/**
 * 编辑新员工
 * @param self
 * @param tiyData
 */
export const modify = (self, tiyData,index) => {
  resetForm(self, tiyData);
  var userInfo = tiyData.tData.userList[index];
  tiyData.tParam.user.id = userInfo.id;
  tiyData.tParam.user.userName = userInfo.userName;
  tiyData.tParam.user.roleId = "";
  tiyData.tParam.user.roleName = "";
  // tiyData.tParam.user.roleId = userInfo.roleId;
  // tiyData.tParam.user.roleName = userInfo.roleName;
  tiyData.tParam.user.access = userInfo.access;
  tiyData.tParam.user.password = '';
  self.modal.isModifyPassword = false;
  self.tempRole = {};
  self.paramUser.roles = [];

  self._.forEach(self.userList[index].employeeRoles,function (item) {
    self.paramUser.roles.push({label:item.name ,value :item.id});
  });

  tiyData.tParam.user.passwordCheck = "";
  tiyData.tStatus.modalModify.isShow = true;
  tiyData.tStatus.modalModify.isModify = true;
  tiyData.tStatus.modalModify.title =   "编辑员工";
};

/**
 * 删除
 * @param self
 * @param tiyData
 */
export const deleted = (self, tiyData,index) => {
  tiyData.tStatus.modalDelete.userInfo.id = tiyData.tData.userList[index].id;
  tiyData.tStatus.modalDelete.userInfo.userName = tiyData.tData.userList[index].userName;
  tiyData.tStatus.modalDelete.isShow = true;
};

/**
 * 添加/修改用户
 * @param self
 * @param tiyData
 * @param name
 */
export const webAddOrModify = function (self, tiyData, name) {
  tiyData.tStatus.modalModify.isLoading = true;

  let roleIds = [];
  self._.forEach(tiyData.tParam.user.roles,function (item) {
    roleIds.push(item.value)
  });

  self.$refs[name].validate((valid) => {
    if (valid) {
      var param = {
        id: tiyData.tParam.user.id,
        username: tiyData.tParam.user.userName,
        password:tiyData.tParam.user.password===''?'': md5(tiyData.tParam.user.password.trim()),
        roleId: "",
        roleName: "",
        roleIds:roleIds,
        access: "1"
      };
      self.$apiRole.addEmployee(param)
        .then(function (response) {
          changeModelLoading(self, tiyData);
          if (response.success) {
            self.$Message.success('提交成功!');
            tiyData.tStatus.modalModify.isShow = false;
            self.$Modal.remove();
            webList(self, tiyData)
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
    } else {
      changeModelLoading(self, tiyData)
    }
  });
};


export const webDelete = function (self, tiyData) {
  tiyData.tStatus.modalDelete.isLoading = true;
  self.$apiRole.deleteEmployee({id: tiyData.tStatus.modalDelete.userInfo.id})
    .then(function (response) {
      tiyData.tStatus.modalDelete.isShow = false;
      changeModelLoading(self, tiyData);
      if (response.success) {
        webList(self, tiyData)
        self.$Message.success('删除成功!');
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiRole.getEmployee({"pageSize": tiyData.tPage.stepNumber, "pageNum": tiyData.tPage.currentPage})
    .then(function (response) {
      self.$Spin.hide();

      if (response.success) {
        tiyData.tData.userList.length = 0;
        tiyData.tPage.total = response.result.total;
        tiyData.tPage.stepNumber = response.result.pageSize;
        response.result.list.forEach(
          function (item) {
            tiyData.tData.userList.push(item)
          }
        )
      } else {
        self.$Message.error(response.errorMessage);
      }
    });

};


export const webRoleList = (self, tiyData) => {
  // self.loadingData = true;
  // self.$Spin.show();
  self.$apiRole.getRoleList({})
    .then(function (response) {
      if (response.success) {
        tiyData.tData.roleList.length = 0;
        let roleList = response.result;
        roleList.forEach(
          function (item) {
            tiyData.tData.roleList.push(item);
          }
        );
        if(0 === tiyData.tData.roleList.length){
          self.$Message.error("查询没有结果");
        }
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};
